using System;
using System.Collections.Generic;

namespace IInject.Registration
{
	/// <summary>
	/// 
	/// </summary>
	/// <typeparam name="T"></typeparam>
	public class EnumerableContextBuilder<T> : DefaultContextBuilder, IEnumerableContextBuilder<T>
	{
		private readonly IList<T> _collection = new List<T>();

		///<summary>
		/// 
		///</summary>
		///<param name="from"></param>
		///<param name="container"></param>
		public EnumerableContextBuilder(Type from, IContainer container) : base(from, container)
		{
		}

		///<summary>
		/// 
		///</summary>
		///<param name="instance"></param>
		public void Add(T instance)
		{
			_collection.Add(instance);
		}

		/// <summary>
		/// The method that registers the <see cref="RegistryContext"/>.
		/// </summary>
		protected override void ApplyCore()
		{
			With(Scope.Singleton);
			var contexts = new List<RegistryContext>();

			for (var i = 0; i < _collection.Count; i++)
			{
				var context = Build();
				context.Instance = _collection[i];
				contexts.Add(context);
			}

            Registry.RegisterAll(FromType, Key, contexts);
		}
	}
}